#! /usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright © 2016 lizongzhe 
#
# Distributed under terms of the MIT license.

import re
import logging
import urllib
import requests
from bs4 import BeautifulSoup
from proxy_manager.core import ProxyHost
from proxy_manager.resource.base import BaseProxyResource

logger = logging.getLogger(__name__)

COUNTRIES = (
        ("any","所有"),
        ("CN","中国"),
        ("DK","丹麦"),
        ("UA","乌克兰"),
        ("UY","乌拉圭"),
        ("AM","亚美尼亚"),
        ("IL","以色列"),
        ("IQ","伊拉克"),
        ("IR","伊朗"),
        ("RU","俄罗斯"),
        ("BG","保加利亚"),
        ("CA","加拿大"),
        ("HU","匈牙利"),
        ("ZA","南非"),
        ("RW","卢旺达"),
        ("IN","印度"),
        ("ID","印度尼西亚"),
        ("EC","厄瓜多尔"),
        ("TW","台湾"),
        ("KG","吉尔吉斯斯坦"),
        ("KZ","哈萨克斯坦"),
        ("CO","哥伦比亚"),
        ("TR","土耳其"),
        ("EG","埃及"),
        ("RS","塞尔维亚"),
        ("SL","塞拉利昂"),
        ("MX","墨西哥"),
        ("DO","多米尼加共和国"),
        ("AT","奥地利"),
        ("VE","委内瑞拉"),
        ("BD","孟加拉国"),
        ("NG","尼日利亚"),
        ("NP","尼泊尔"),
        ("PK","巴基斯坦"),
        ("PY","巴拉圭"),
        ("PA","巴拿马"),
        ("BR","巴西"),
        ("GR","希腊"),
        ("DE","德国"),
        ("IT","意大利"),
        ("LV","拉脱维亚"),
        ("CZ","捷克"),
        ("MD","摩尔多瓦"),
        ("SK","斯洛伐克"),
        ("SG","新加坡"),
        ("JP","日本"),
        ("CL","智利"),
        ("KH","柬埔寨"),
        ("GE","格鲁吉亚"),
        ("EU","欧洲"),
        ("BE","比利时"),
        ("FR","法国"),
        ("PL","波兰"),
        ("TH","泰国"),
        ("ZW","津巴布韦"),
        ("AU","澳大利亚"),
        ("MO","澳门"),
        ("TT","特里尼达和多巴哥"),
        ("BO","玻利维亚"),
        ("SE","瑞典"),
        ("CH","瑞士"),
        ("PE","秘鲁"),
        ("RO","罗马尼亚"),
        ("US","美国"),
        ("KE","肯尼亚"),
        ("GB","英国"),
        ("NL","荷兰"),
        ("PH","菲律宾"),
        ("ES","西班牙"),
        ("ZM","赞比亚"),
        ("VN","越南"),
        ("AZ","阿塞拜疆"),
        ("AL","阿尔巴尼亚"),
        ("AR","阿根廷"),
        ("AE","阿联酋"),
        ("KR","韩国"),
        ("HK","香港"),
        ("MV","马尔代夫"),
        ("MY","马来西亚"),
        ("LB","黎巴嫩"),
)

SCHEMES = (
    ('https', 'https'),
    ('http', 'http'),
)

AVAILABILITY = [(value, ">={}%".format(value)) for value in range(0, 100, 10)]

class ProxyResource(BaseProxyResource):
    url = 'http://www.kuaidaili.com/proxylist/'
    def __init__(self, country='', scheme='http', availability=70):
        self.country = country
        self.scheme = scheme
        self.availability = availability

    def download(self):
        if self.country == 'CN':
            if self.scheme == 'http':
                url = 'http://www.kuaidaili.com/free/inha/'
                page_num = 2
            else:
                url = 'http://www.kuaidaili.com/proxylist/'
                page_num = 4
        else:
            if self.scheme == 'http':
                url = 'http://www.kuaidaili.com/free/outha/'
                page_num = 8
            else:
                return []

        resps = []
        for i in range(page_num):
            resps.append(requests.get(url + '{}/'.format(i+1)))

        return resps

    def parse(self, pages):
        country_name = [c[1] for c in COUNTRIES if c[0] == self.country][0]
        result = []
        for page in pages:
            soup = BeautifulSoup(page.content, 'html.parser')

            for proxy_elem in soup.select('table tr'):
                try:
                    proxy_field_elems = proxy_elem.select('td')
                    if len(proxy_field_elems) == 0:
                        continue

                    if self.country != 'CN':
                        cont = proxy_field_elems[4].text.strip(' ').encode('utf-8')
                        if cont != country_name:
                            continue
                    elif self.scheme == 'https':
                        if 'HTTPS' not in proxy_field_elems[3].text:
                            continue

                    ip = urllib.unquote_plus(proxy_field_elems[0].text)
                    port = proxy_field_elems[1].text
                    proxy_host = ProxyHost(ip=ip, port=port, scheme=self.scheme)
                    result.append(proxy_host)
                except Exception as e:
                    logger.error(e, exc_info=True)
        return result

    def get_proxies(self):
        try:
            pages = self.download()
            data = self.parse(pages)
            return data
        except Exception as e:
            logger.error(e, exc_info=True)
            raise e



